﻿#pragma once
#include<iostream>
using namespace std;
// 枚举值表⽰颜⾊
enum Colour
{
	RED,
	BLACK
};
// 这⾥我们默认按key/value结构实现
template<class T>
struct RBTreeNode
{
	// 这⾥更新控制平衡也要加⼊parent指针
	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;
	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		,_col(RED) //新插入的节点给红色
	{}
};


template<class T,class Ref,class Ptr>
class RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;
public:
	RBTreeIterator(Node* node, Node* root)
		:_node(node)
		, _root(root)
	{}
	Self operator++()
	{
		if (_node->_right)
		{
			// 右不为空，中序下一个访问的节点是右子树的最左(最小)节点
			Node* min = _node->_right;
			while (min->_left)
			{
				min = min->_left;
			}
			_node = min;
		}
		else
		{
			// 右为空，祖先里面孩子是父亲左的那个祖先
			Node* cur = _node;
			Node* parent = cur->_parent;
			while(parent&&cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
	Self operator--()
	{
		if (_node == nullptr)	// --end()
		{
			// --end()，特殊处理，走到中序最后一个结点，整棵树的最右结点
			Node* cur = _root;
			while (cur->_right)
			{
				cur = cur->_right;
			}
			_node = cur;
		}
		else if (_node->_left)
		{
			// 左子树不为空，中序左子树最后一个
			Node* min = _node->_left;
			while (min->_right)
			{
				min = min->_right;
			}
			_node = min;
		}
		else
		{
			// 孩子是父亲右的那个祖先
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}
	bool operator==(const Self& s) const
	{
		return _node == s._node;
	}
	bool operator!=(const Self& s) const
	{
		return _node != s._node;
	}
private:
	Node* _node;
	Node* _root;
};

template<class K, class T, class KeyOfT >
class RBTree
{
	
public:
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, T&, T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> Const_Iterator;
	RBTree() = default;
	RBTree(const RBTree<K,T, KeyOfT>& t)
	{
		_root = copy(t._root);
	}
	~RBTree()
	{
		destroy(_root);
		_root = nullptr;
	}
	RBTree<K, T, KeyOfT>& operator=(RBTree<K, T, KeyOfT> t)
	{
		swap(_root, t._root);
		return *this;
	}
	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur, _root);
	}
	Iterator End()
	{
		return Iterator(nullptr, _root);
	}
	Const_Iterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur, _root);
	}
	Const_Iterator End() const
	{
		return Iterator(nullptr, _root);
	}

	Iterator Find(const T& data)
	{
		Node* cur = _root;
		KeyOfT kot;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				cur = cur->_left;
			}
			else
			{
				return Iterator(cur,_root);
			}
		}
		return End();
	}
	pair<Iterator,bool> insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return { Iterator(_root,_root),true };
		}
		else
		{
			Node* cur = _root;
			Node* parent = nullptr;
			KeyOfT kot;
			while (cur)
			{
				if (kot(data) > kot(cur->_data))
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (kot(data) < kot(cur->_data))
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return { Iterator(cur,_root),false };
				}
			}
			cur = new Node(data);
			Node* newnode = cur;//防止后面因变色改变cur的指向
			if (kot(data) > kot(parent->_data))
			{
				parent->_right = cur;
			}
			else
			{
				parent->_left = cur;
			}
			cur->_parent = parent;
			//如果⽗亲结点是红⾊的,需要根据u分为以下⼏种情况分别处理。
			while (parent && parent->_col == RED)
			{
				Node* grandfather = parent->_parent;
				//叔叔在爷爷的右边
				if (grandfather->_left == parent)
				{
					Node* uncle = grandfather->_right;
					//叔叔存在且为红,则变色。
					//	  g
					//  p   u
					//c
					//c在p的左或右都一样
					if (uncle && uncle->_col == RED)
					{
						parent->_col = uncle->_col = BLACK;
						grandfather->_col = RED;
						cur = grandfather;
						parent = cur->_parent;
					}
					//叔叔不存在或存在为黑，则旋转+变色
					else
					{
						//c在p的左边，则单旋+变色
						//	  g
						//  p   u
						//c
						if (cur == parent->_left)
						{
							RotateR(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;
						}
						//c在p的右边，则双旋+变色
						//	  g
						//  p   u
						//	  c
						else
						{
							RotateL(parent);
							RotateR(grandfather);
							cur->_col = BLACK;
							grandfather->_col = RED;
						}
						break;
					}
				}
				//叔叔在爷爷的左边
				else
				{
					Node* uncle = grandfather->_left;
					//叔叔存在且为红,则变色。
					//	  g
					//  u   p
					//		  c
					//c在p的左或右都一样
					if (uncle && uncle->_col == RED)
					{
						parent->_col = uncle->_col = BLACK;
						grandfather->_col = RED;
						cur = grandfather;
						parent = cur->_parent;
					}
					//叔叔不存在或存在为黑，则旋转+变色
					else
					{
						//c在p的右边，则单旋+变色
						//	  g
						//  u   p
						//		  c
						if (cur == parent->_right)
						{
							RotateL(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;
						}
						//c在p的右边，则双旋+变色
						//	  g
						//  u   p
						//	  c
						else
						{
							RotateR(parent);
							RotateL(grandfather);
							cur->_col = BLACK;
							grandfather->_col = RED;
						}
						break;
					}
				}
			}
			_root->_col = BLACK;
			return { Iterator(newnode,_root),true };
		}
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		parent->_left = subLR;
		subL->_right = parent;

		Node* Pparent = parent->_parent;
		parent->_parent = subL;
		if (subLR)
		{
			subLR->_parent = parent;
		}
		if (parent == _root)
		{
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
			if (parent == Pparent->_left)
			{
				Pparent->_left = subL;
			}
			else 
			{
				Pparent->_right = subL;
			}
			subL->_parent = Pparent;
		}
	}
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		subR->_left = parent;
		
		Node* Pparent = parent->_parent;
		parent->_parent = subR;
		if (subRL)
		{
			subRL->_parent = parent;
		}
		if (parent == _root)
		{
			_root = subR;
			_root->_parent = nullptr;
		}
		else
		{
			if (parent == Pparent->_left)
			{
				Pparent->_left = subR;
			}
			else
			{
				Pparent->_right = subR;
			}
			subR->_parent = Pparent;
		}
	}
	
private:
	void copy(Node* root)
	{
		if (root == nullptr) {
			return;
		}
		Node* newnode = new Node(root->_data);
		copy(root->_left);
		copy(root->_right);
	}
	void destroy(Node* root)
	{
		if (root == nullptr) {
			return;
		}
		destroy(root->_left);
		destroy(root->_right);
		delete root;
	}
	Node* _root = nullptr;
};

